﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfumeShopManagement.Model;

namespace PerfumeShopManagement.DAL
{
    public class DalImportCheck : DalAbstractBase<ImportCheck, int>
    {
        public override ImportCheck GetObjectById(int id)
        {
            throw new NotImplementedException();
        }

        public override List<ImportCheck> GetAllObjects()
        {
            var result = (from i in _context.ImportChecks
                          select i);
            if (result != null)
                return result.ToList<ImportCheck>();

            return new List<ImportCheck>();
        }

        public override List<ImportCheck> GetObjectsByCriteria(ImportCheck obj)
        {
            throw new NotImplementedException();
        }

        public override bool Insert(ImportCheck obj)
        {
            ImportCheck importCheck = obj;

            _context.ImportChecks.AddObject(importCheck);
            _context.SaveChanges();

            return true;
        }

        public override bool Update(ImportCheck obj)
        {
            ImportCheck importCheck = obj;

            // Delete all import check detail of the import check.
            var detailDeleting = (from i in _context.ImportCheckDetails
                                  where i.ImportCheckDetailId.Equals(importCheck.ImportCheckId)
                                  select i);
            foreach (var item in detailDeleting)
            {
                _context.ImportCheckDetails.DeleteObject(item);
            }


            // Update the import check.
            ImportCheck importCheckRow = _context.ImportChecks.Single(
                e => e.ImportCheckId == importCheck.ImportCheckId);
            importCheckRow.ImportCheckTime = importCheck.ImportCheckTime;
            importCheckRow.Total = importCheck.Total;
            importCheckRow.ImportCheckDetail = importCheck.ImportCheckDetail;

            _context.SaveChanges();

            // Refresh the import check detail to return.
            foreach (ImportCheckDetail im in importCheckRow.ImportCheckDetail)
            {
                importCheck.ImportCheckDetail.Add(im);
            }

            return true;
        }

        public override bool DeleteById(int id)
        {
            throw new NotImplementedException();
        }
    }
}
